//**************************************************************
//		Node.m
//			AppHider
//
//	Copyright (c) 2008 Metta Media
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//**************************************************************
#import "Node.h"


@implementation Node
- (id)init
{
    [super init];
    children = [[NSMutableArray alloc] init];
	itemName = [[NSMutableArray alloc] init];
	itemAction = [[NSMutableArray alloc] init];
	itemValue = [[NSMutableArray alloc] init];
	itemPreferences = [[NSMutableDictionary alloc] init];
    return self;
}

- (id)initWithName:(NSString *)name Action:(NSString *)action Value:(id)value Preferences:(NSMutableDictionary *)preferences Parent:(NSString *)parent
{
    [super init];
    children = [[NSMutableArray alloc] init];
	itemName = name;
	[itemName retain];
	itemAction = action;
	[itemAction retain];
	if([value isKindOfClass:[NSMutableArray class]]) {
		itemValue = nil;
		children = value;
	}
	else if([value isKindOfClass:[NSString class]])
	{
		itemValue = value;
		[itemValue retain];
	}
	else
		@throw NSInvalidArgumentException;
	itemPreferences = preferences;
	[itemPreferences retain];
	NSLog(@"item pref node are: %@", itemPreferences);
	parentID = parent;
	[parentID retain];
	return self;
}

-(void)setParentID:(NSString *)i
{
	[i retain];
	[parentID release];
	parentID = i;
	[parentID retain];
}

-(NSString*)parentID
{
	return parentID;
}

-(void) awakeFromNib
{
  // Nothing to see here...
}
  
- (void)setItemName:(NSString *)s
{
    [s retain];
    [itemName release];
    itemName = s;
	[itemName retain];
}

- (NSString *)itemName
{
    return itemName;
}

- (void)setItemAction:(NSString *)a
{
    [a retain];
    [itemAction release];
    itemAction = a;
	[itemAction retain];
}

- (NSString *)itemAction
{
    return itemAction;
}

- (void)setItemValue:(NSString *)v
{
    [v retain];
    [itemValue release];
    itemValue = v;
	[itemValue retain];
}

- (NSString *)itemValue
{
    return itemValue;
}

- (void)setItemPreferences:(NSMutableDictionary *)p
{
    [p retain];
    [itemPreferences release];
    itemPreferences = p;
	[itemPreferences retain];
}

- (NSMutableDictionary *)returnItemPreferences
{
	[itemPreferences retain];
	NSLog(@"the prefs that are being returned are: ", itemPreferences);
	return (NSMutableDictionary*)itemPreferences;
}

- (void)addChildNode:(Node *)n
{
    [children addObject:n];
}

- (void)insertChildNode:(Node *)n atIndex:(int)i
{
	[children insertObject:n atIndex:i];
}

- (void)setChildren:(NSMutableArray *)a
{
	[a retain];
    [children release];
    children = a;
}

- (NSMutableArray*)returnChildren
{
	return children;
}

- (Node *)childAtIndex:(int)i
{
    return [children objectAtIndex:i];
}
- (int)indexOfChild:(id)n
{
	return [children indexOfObject:n];
}
- (void)replaceChild:(id)old withChild:(id)n
{
	[children replaceObjectAtIndex:[children indexOfObject:old] withObject:n];
}
- (void)replaceChildAtIndex:(int)i withChild:(id)n
{
	[children replaceObjectAtIndex:i withObject:n];
}
- (void)removeChild:(id)n
{
	[children removeObject:n];
}
- (int)childrenCount
{
    return [children count];
}
- (BOOL)expandable
{
    //return ([children count] > 0);
	if([itemAction isEqualToString:@"Submenu"])
	{
		return YES;
	}
	else{
		return NO;
	}
}

- (void)dealloc
{
    [children release];
    [itemName release];
	[itemAction release];
	[itemValue release];
	[itemPreferences release];
    [super dealloc];
}
@end
